package com.kwxy.hyt.controller;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.kwxy.hyt.base.BaseController;
import com.kwxy.hyt.po.*;
import com.kwxy.hyt.utils.Pager;
import java.util.*;

import com.kwxy.hyt.po.*;
import com.kwxy.hyt.mapper.*;
import com.kwxy.hyt.service.*;

/**
 * @ClassName:  
 * @Description: 
 * @author  - - admin
 * @date - 2020年9月10日 09时48分24秒
 */


@Controller
@RequestMapping("/student")
public class StudentController extends BaseController {
	
	@Autowired
	private BjService bjService;
	/**
	 * 依赖注入 start dao/service/===
	 */
	@Autowired
	private StudentService studentService;
	
	@Autowired
	private FdyService fdyService;
	
	@Autowired
	private RecordService recordService;
	
	// --------------------------------------- 华丽分割线 ------------------------------
	
	
	/**
	 * 分页查询 返回list对象(通过对By Sql)
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/findBySql")
	public String findBySql(Student student, Model model, HttpServletRequest request, HttpServletResponse response) {
		
		Object attribute = request.getSession().getAttribute("userId");
		
		if (attribute == null){
			return "redirect:/login/uLogin";	
		}
		Integer userId = Integer.valueOf(attribute.toString());
		Fdy byId = fdyService.getById(userId);
		if (byId== null || byId.getBjId() == null){
			return "redirect:/login/message?type=1";
		}
		//分页查询
		String sql = "SELECT * FROM student WHERE isDelete = 0 and  bjId = "+ byId.getBjId();
        if(!isEmpty(student.getCode())){
        	sql += " and code like '%"+student.getCode()+"%'";
		}
        if(!isEmpty(student.getUsername())){
        	sql += " and username like '%"+student.getUsername()+"%'";
		}
        if(!isEmpty(student.getPassword())){
        	sql += " and password like '%"+student.getPassword()+"%'";
		}
        if(!isEmpty(student.getBjId())){
        	sql += " and bjId like '%"+student.getBjId()+"%'";
		}
        if(!isEmpty(student.getIsDelete())){
        	sql += " and isDelete like '%"+student.getIsDelete()+"%'";
		}
        sql += " ORDER BY ID DESC ";
		Pager<Student> pagers = studentService.findBySqlRerturnEntity(sql);
		model.addAttribute("pagers", pagers);
		//存储查询条件
		model.addAttribute("obj", student);
		return "student/student";
	}
	
	@RequestMapping(value = "/pass")
	public String pass(Integer id,HttpServletRequest request,Model model) {
       Object attribute = request.getSession().getAttribute("userId");
		
		if (attribute == null){
			return "redirect:/login/uLogin";	
		}
		Integer userId = Integer.valueOf(attribute.toString());
		Student obj = studentService.load(userId);
		model.addAttribute("obj",obj);
		return "student/pass";
	}
	
	
	@RequestMapping("/upass")
	@ResponseBody
	public String upass(HttpServletRequest request,String password){
      Object attribute = request.getSession().getAttribute("userId");
      JSONObject j = new  JSONObject();
	   	if (attribute == null){
	   		j.put("res", 0);
	   		return j.toString();
		}
			Integer userId = Integer.valueOf(attribute.toString());
			Student byId = studentService.getById(userId);
			byId.setPassword(password);
			studentService.updateById(byId);
			j.put("res", 1);
			return j.toString();
			
	}
	
	/**
	 * 分页查询 返回list对象(通过Map)
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/findByMap")
	public String findByMap(Student student, Model model, HttpServletRequest request, HttpServletResponse response) {
		//通过map查询
		Map<String,Object> params = new HashMap<String,Object>();
        if(!isEmpty(student.getCode())){
        	params.put("code", student.getCode());
		}
        if(!isEmpty(student.getUsername())){
        	params.put("username", student.getUsername());
		}
        if(!isEmpty(student.getPassword())){
        	params.put("password", student.getPassword());
		}
        if(!isEmpty(student.getBjId())){
        	params.put("bjId", student.getBjId());
		}
        if(!isEmpty(student.getIsDelete())){
        	params.put("isDelete", student.getIsDelete());
		}
		//分页查询
		Pager<Student> pagers = studentService.findByMap(params);
		model.addAttribute("pagers", pagers);
		//存储查询条件
		model.addAttribute("obj", student);
		return "student/student";
	}
	
	/**********************************【增删改】******************************************************/
	
	/**
	 * 跳至添加页面
	 * @return
	 */
	@RequestMapping(value = "/add")
	public String add() {
		return "student/add";
	}

	/**
	 * 跳至详情页面
	 * @return
	 */
	@RequestMapping(value = "/view")
	public String view(Integer id,Model model) {
		Student obj = studentService.load(id);
		model.addAttribute("obj",obj);
		return "student/view";
	}
	
	/**
	 * 添加执行
	 * @return
	 */
	@RequestMapping(value = "/exAdd")
	public String exAdd(Student student, Model model, HttpServletRequest request, HttpServletResponse response) {

		Object attribute = request.getSession().getAttribute("userId");
		
		if (attribute == null){
			return "redirect:/login/uLogin";	
		}
		Integer userId = Integer.valueOf(attribute.toString());
		Fdy byId = fdyService.getById(userId);
		if (byId== null || byId.getBjId() == null){
			return "redirect:/login/message?type=1";
		}
		student.setBjId(byId.getBjId());
		student.setIsDelete(0);
		studentService.insert(student);
		return "redirect:/student/findBySql.action";
	}
	
	
	/**
	 * 跳至修改页面
	 * @return
	 */
	@RequestMapping(value = "/update")
	public String update(Integer id,Model model) {
		Student obj = studentService.load(id);
		model.addAttribute("obj",obj);
		return "student/update";
	}
	
	/**
	 * 添加修改
	 * @return
	 */
	@RequestMapping(value = "/exUpdate")
	public String exUpdate(Student student, Model model, HttpServletRequest request, HttpServletResponse response) {
		//1.通过实体类修改，可以多传修改条件
		studentService.updateById(student);
		//2.通过主键id修改
		//studentService.updateById(student);
		return "redirect:/student/findBySql.action";
	}
	
	/**
	 * 删除通过主键
	 * @return
	 */
	@RequestMapping(value = "/delete")
	public String delete(Integer id, Model model, HttpServletRequest request, HttpServletResponse response) {
		Student load = studentService.load(id);
		load.setIsDelete(1);
		studentService.updateById(load);
		return "redirect:/student/findBySql.action";
	}


	
}
